package week01

object HW1{
  def main() {
    println("Pascal's Triangle")
    for (row <- 0 to 10) {
      for (col <- 0 to row)
        print(pascal(col, row) + " ")
      println()
    }
  }                                               //> main: ()Unit

  /**
   * Exercise 1
   */
  def pascal(c: Int, r: Int): Int = {
//    if(r==0) 1
//    if((1==c)||(r==c)) 1

	  if (c>r) -1
	  
    if((0==c)||(r==c)) 1
    else pascal(c-1,r-1)+pascal(c,r-1)
  }                                               //> pascal: (c: Int, r: Int)Int
  

  /**
   * Exercise 2
   */
  def balance(chars: List[Char]): Boolean ={
		
		var count:Int = 0
		
    def bal(chars: List[Char]): Boolean = {
    	
      if (chars.isEmpty && count==0) true
      else if (chars.isEmpty && count!=0) false
      else{
				println(chars)
		    
	      if (count >= 0) {
	        if (equalLeft(chars.head)) count = count + 1
	        if (equalRight(chars.head)) count = count - 1
	        bal(chars.tail)
	      } else {
	        false
	      }
      }
    }
    def equalLeft(c:Char): Boolean = '('.equals(c)
    def equalRight(c:Char): Boolean = ')'.equals(c)
    
    bal(chars)
  }                                               //> balance: (chars: List[Char])Boolean
		
    
  /**
   * Exercise 3
   */
  def countChange(money: Int, coins: List[Int]): Int = {
  	/*
  	def count(money: Int,coins: List[Int]): Int={
	  	if(money==0) 1
	  	
	  	if(money<0) 0
	  	
	  	if(money>=1 && coins.isEmpty) 0
	  	println(money,coins)
	  	count(money,coins.tail ) + count(money-coins.head, coins)
	  	}
  	*/
  		def count(money: Int,coins: List[Int]): Int={
		  	if(money==0) 1
		  	
		  	else if(money<0) 0
		  	
		  	else if(money>=1 && coins.length<=0) 0 //coins.isEmpty
				else
		  		count(money,coins.tail ) + count(money-coins.head, coins)
  		}
  	
  	count(money,coins)
  }                                               //> countChange: (money: Int, coins: List[Int])Int
  
  
  val nums: List[Int] = List(1,2,4,3,5)           //> nums  : List[Int] = List(1, 2, 4, 3, 5)
  nums.tail                                       //> res0: List[Int] = List(2, 4, 3, 5)
  
  balance(")(".toList)                            //> List(), ()
                                                  //| List(()
                                                  //| res1: Boolean = false
  countChange(4,List(1,2,3))                      //> res2: Int = 4
  countChange(10,List(2,5,3,6))                   //> res3: Int = 5
  countChange(1,List(2,5,3,6))                    //> res4: Int = 0
}